const path = require('path')
const productionGzipExtensions = ['js', 'css']
const CompressionPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

function resolve(dir) {
  return path.join(__dirname, dir)
}

/**
 * @param {string} baseUrl 公共路径
 * @param {string} outputDir 输出目录
 * @param {string} assetsDir 用于嵌套生成静态资产的目录
 * @param {string} productionSourceMap 是否构建map文件
 */
module.exports = {
  baseUrl: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false,

  chainWebpack: config => {
    config.resolve.extensions.add('.css')
    config.plugin('html').tap(args => {
      args[0].chunksSortMode = 'none'
      return args
    })
    // config.module
    //   .rule('iview')
    //   .test(/iview.src.*?js$/)
    //   .use('babel')
    //     .loader('babel-loader')
  },

  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.plugins.push(
        new CompressionPlugin({
          asset: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
        // new BundleAnalyzerPlugin()
      )
    }
  },

  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/assets/css/index.scss";`
      }
    }
  },

  // configure webpack-dev-server behavior
  // devServer: {
    // open: process.platform === 'darwin',
    // host: '0.0.0.0',
    // port: 8080,
    // https: false,
    // hot: true,
    // hotOnly: true,
    // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#configuring-proxy
    // proxy: null, // string | Object
    // before: app => {},
  //   watchOptions: {
  //     ignored: '/node_modules/',
  //     poll: 1200
  //   }
  // },
  // // options for 3rd party plugins
  pluginOptions: {
    // ...
  }
}
